package cn.fl.splmsg.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.push.api.IPushApiService;
import cmm.user.facade.push.dto.PushDTO;
import cmm.user.facade.system.dto.LoginCacheDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.product.transform.SplInfoVOTransform;
import cn.fl.product.vo.SplInfoVO;
import cn.fl.splmsg.condition.SplBullMsgLMQueryCondition;
import cn.fl.splmsg.constant.SplBullMsgLMConstant;
import cn.fl.splmsg.dto.SplBullMsgLMDTO;
import cn.fl.splmsg.service.ISplBullMsgLMApiService;
import cn.fl.splmsg.service.ISplBullMsgLMService;
import cn.fl.splmsg.transform.SplBullMsgLMVOTransform;
import cn.fl.splmsg.vo.SplBullMsgLMVO;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import fl.spl.facade.spl.api.ISplInfoApiService;
import fl.spl.facade.spl.condition.SplInfoQueryCondition;

/**
 * @Description: 供应商公告消息日志主表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class SplBullMsgLMServiceImpl implements ISplBullMsgLMService {

	/**
	 * 供应商公告消息日志主表 暴露服务
	 */
	@Autowired
	private ISplBullMsgLMApiService splBullMsgLMApiService;
	/**
	 * 供应商基础信息  服务
	 */
	@Autowired
	private ISplInfoApiService splInfoApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    /**
     * 区域服务
     */
    @Autowired
    private ISysRegionApiService sysRegionApiService;
    
    @Autowired
    private IPushApiService pushApiService;

	@Override
	public SplBullMsgLMVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return null;
		}
		SplBullMsgLMDTO dto = this.splBullMsgLMApiService.selectSplBullMsgLMById(id);
		return SplBullMsgLMVOTransform.toVO(dto);
	}

	@Override
	public Long add(SplBullMsgLMVO entity) throws BaseException{
		if(null == entity){
			return null;
		}
		return this.splBullMsgLMApiService.addSplBullMsgLM(SplBullMsgLMVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			return false;
		}
		return this.splBullMsgLMApiService.removeSplBullMsgLMByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(SplBullMsgLMVO entity) throws BaseException{
		if(null == entity){
			return false;
		}
		return this.splBullMsgLMApiService.modifySplBullMsgLM(SplBullMsgLMVOTransform.toDTO(entity));
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        SplBullMsgLMQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new SplBullMsgLMQueryCondition());
        }
		condition=(SplBullMsgLMQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.splBullMsgLMApiService.searchSplBullMsgLMListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<SplBullMsgLMVO> voList=SplBullMsgLMVOTransform.toVOList((List<SplBullMsgLMDTO>)page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<SplBullMsgLMVO> searchList(SplBullMsgLMQueryCondition condition) throws BaseException{
		if(null == condition){
			condition=new SplBullMsgLMQueryCondition();
		}
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<SplBullMsgLMVO> voList=SplBullMsgLMVOTransform.toVOList(this.splBullMsgLMApiService.searchSplBullMsgLMList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<SplBullMsgLMVO> voList){
		if(CollectionUtils.isEmpty(voList)){
			return;
		}
		Map<String, String> _CdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), SplBullMsgLMConstant.PUB_STS_CD_CODE);
		for(SplBullMsgLMVO vo:voList){
			if (_CdMap != null && _CdMap.containsKey(vo.getPubStsCd())) {
            	vo.setPubStsCd(_CdMap.get(vo.getPubStsCd()));
        	}	
		}
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
	@Override
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }
	    
    @SuppressWarnings("unchecked")
	@Override
	public Pager searchSplBullAlreadySendListPage(Pager page) throws BaseException{
        if(null == page) {
            page = new Pager();
            page.setCondition(new SplBullMsgLMQueryCondition());
        }
//        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
//        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
//        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.splBullMsgLMApiService.searchSplBullMsgAlreadySendListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<SplBullMsgLMVO> voList=SplBullMsgLMVOTransform.toVOList((List<SplBullMsgLMDTO>)page.getResultList());
            this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}

	@Override
	public JSONArray searchSplInfoList(SplInfoQueryCondition condition) throws BaseException {
		if(null == condition){
			condition=new SplInfoQueryCondition();
		}
		List<SplInfoVO> voList = SplInfoVOTransform.toVOList(splInfoApiService.searchSplInfoList(condition));
		JSONArray ja = new JSONArray();
		for (int i = 0; i < voList.size(); i++) {
			JSONObject jo = new JSONObject();
			jo.put("splId", voList.get(i).getId().toString());
			jo.put("splNm", voList.get(i).getSplNm());
			ja.add(jo);
		}
		return ja;
	}

	@Override
	public List<SplBullMsgLMVO> searchSplBullPushList(SplBullMsgLMQueryCondition splBullMsgLMQueryCondition)
			throws BaseException {
		if(splBullMsgLMQueryCondition==null){
			splBullMsgLMQueryCondition = new SplBullMsgLMQueryCondition();
		}
		List<SplBullMsgLMVO> listVo = SplBullMsgLMVOTransform.toVOList(splBullMsgLMApiService.selectPushSplUsrList(splBullMsgLMQueryCondition));
		return listVo;
	}

	@Override
	public Map<String, LoginCacheDTO> getLoginCahceDTO(List<String> loginNm)
			throws BaseException {
		return this.splBullMsgLMApiService.getLoginCahceDTO(loginNm);
	}

	@Override
	public Map<String, String> getLoginCahceDTOToMap(List<String> loginNm)
			throws BaseException {
		return this.splBullMsgLMApiService.getLoginCahceDTOToMap(loginNm);
	}

	@Override
	public DataResultDTO pushToDeviceToken(PushDTO entity) {
		return this.pushApiService.pushToDeviceToken(entity);
	}

	@Override
	public DataResultDTO pushToDeviceTokenList(PushDTO entity) {
		return this.pushApiService.pushToDeviceTokenList(entity);
	}
	
}
